	package importadodebookworm;

import java.util.LinkedList;

public class GetTagContent
{
	public String getTagContent(String message, String tagName)
	{
		String openTag = "<" + tagName + ">";
		String closeTag = "</" + tagName + ">";
		int openTagIndex = message.indexOf(openTag);
		int closeTagIndex = message.indexOf(closeTag);
		if(openTagIndex == -1 || closeTagIndex == -1)
		{
			return "";
		}
		
		String answerWithTags = message.substring(openTagIndex, closeTagIndex);
		//now let's remove the openTag
		String answerWithoutTags = answerWithTags.replace(openTag, "");
		return answerWithoutTags;
	}
	public LinkedList<String> getTagContentsMultipleOccurrences(String message, String tagName)
	{
		int index = 0;
		LinkedList<String> contents = new LinkedList<String>();
		while(message != "" && index < message.length())
		{
			String openTag = "<" + tagName + ">";
			String closeTag = "</" + tagName + ">";
			int openTagIndex = message.indexOf(openTag);
			int closeTagIndex = message.indexOf(closeTag);
			if(closeTagIndex == -1 || openTagIndex == -1)
			{
				break;
			}
			String contentWithTags = message.substring(openTagIndex, closeTagIndex);
			//now let's remove the open tag
			String contentWithoutTags = contentWithTags.replace(openTag, "");
			contents.add(contentWithoutTags);
			
			//now let's continue the search
			message = message.substring(closeTagIndex + closeTag.length() - 1);
		}
		
		return contents;
	}
	
	public String getTagContentWithType(String message, String tagName, String type)
	{
		String openTag = "<" + tagName + " type=\"" + type +  "\">";
		String closeTag = "</" + tagName + ">";
		int openTagIndex = message.indexOf(openTag);
		
		/*Now i have to get the closeTag index, but i can have multiple ocurrences of "/tagName".
		  That's why i will get everything on message that's after the openTagIndex and 
		  put it in a substring. That way, i can get the closeTagIndex from the closing tag
		  that's right after the openTag i already found*/ 
		
		String messageAfterOpenTag = message.substring(openTagIndex);
		
		int closeTagIndexOnMessageAfterOpenTag = messageAfterOpenTag.indexOf(closeTag);
		
		if(openTagIndex == -1 || closeTagIndexOnMessageAfterOpenTag == -1)
		{
			return "";
		}
		
		/*I have created another String besides message, it was "messageAfterOpenTag".
		Because of that, my closeTagIndex is referenced to the begginning of 
		the "messageAfterOpenTag", not the usual "message". So, i will sum the 
		openTagIndex(from "message") and the closeTagIndex(from "messageAfterOpenTag")
		and get the real closeTagIndex from "message"*/
		
		int closeTagIndexFromMessage = openTagIndex +  closeTagIndexOnMessageAfterOpenTag;
		
		String answerWithTags = message.substring(openTagIndex, closeTagIndexFromMessage);
		//now let's remove the openTag
		String answerWithoutTags = answerWithTags.replace(openTag, "");
		return answerWithoutTags;
	}
	
}
